<%--
  Created by IntelliJ IDEA.
  User: 24258
  Date: 2022/8/30
  Time: 23:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="ztree/zTreeStyle.css">
<script type="text/javascript" src="ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="crowd/my-auth.js"></script>
<script type="text/javascript">
    $(function () {
        generateTree();

        // 添加子节点绑定响应事件
        $("#authTreeDemo").on("click", ".addBtn", function () {
            $("#authAddModal").modal("show");
            window.categoryId = this.id;
            return false;
        })


        // 绑定提交添加按钮
        $("#authSaveBtn").click(function () {
            var title = $.trim($("#authAddModal [name=title]").val());
            var name = $.trim($("#authAddModal [name=name]").val());

            $.ajax({
                url: "auth/save.json",
                type: "post",
                data: {
                    "categoryId": window.categoryId,
                    "title": title,
                    "name": name
                },
                dataType: "json",
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("添加成功");
                        // 清理模态框
                        $("#authResetBtn").click();
                        // 重新加载页面
                        generateTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("添加失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#authAddModal").modal("hide");

        })

        // 修改子节点绑定响应事件
        $("#authTreeDemo").on("click", ".editBtn", function () {
            $("#authEditModal").modal("show");
            var zthreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            window.id = this.id;
            var key = "id";
            var value = window.id;
            var currentNode = zthreeObj.getNodeByParam(key, value);

            $("#authEditModal [name=title]").val(currentNode.title);
            $("#authEditModal [name=name]").val(currentNode.name);
            return false;
        })

        // 更新
        $("#authEditBtn").click(function () {
            var title = $.trim($("#authEditModal [name=title]").val());
            var name = $.trim($("#authEditModal [name=name]").val());
            // console.log(title);
            // console.log(name)
            $.ajax({
                url: "auth/edit.json",
                type: "POST",
                dataType: "json",
                data: {
                    "id": window.id,
                    "title": title,
                    "name": name
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {

                        layer.msg("更新成功");
                        // 重新加载页面
                        generateTree();
                        return;
                    }

                    if (result == "FAILED") {
                        layer.msg("更新失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#authEditModal").modal("hide");

        })

        $("#authTreeDemo").on("click", ".removeBtn", function () {
            window.id = this.id;
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");
            var currentNode = zTreeObj.getNodeByParam("id", window.id);
            var title = currentNode.title;
            $("#removeNodeSpan").html("&nbsp;" + title + "&nbsp;");
            $("#authConfirmModal").modal("show");
            return false;
        })

        $("#confirmBtn").click(function () {

            $.ajax({
                url: "auth/remove.json",
                type: "POST",
                dataType: "json",
                data: {
                    "id": window.id,
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("删除成功");
                        // 重新加载页面
                        generateTree();
                        return;
                    }
                    if (result == "FAILED") {
                        layer.msg("删除失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#authConfirmModal").modal("hide");
        })
        //功能添加模块
        $("#addRootAuthBtn").click(function () {
            $("#authAddRootModal").modal("show");
        })
        $("#addRootAuthModalBtn").click(function (){
            var title = $.trim($("#authAddRootModal [name=title]").val());


            $.ajax({
                url: "auth/root/save.json",
                type: "POST",
                dataType: "json",
                data: {
                    "title":title
                },
                success: function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("删除成功");
                        $("#authAddRootModal [name=title]").val("")
                        // 重新加载页面
                        generateTree();
                        return;
                    }
                    if (result == "FAILED") {
                        layer.msg("删除失败" + response.message);
                        return;
                    }

                },
                error: function (response) {
                    layer.msg("错误代码：" + response.status + ",错误信息：" + response.statusText);
                }
            })
            $("#authAddRootModal").modal("hide");


        })

    })
</script>

<body>
<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <div class="panel panel-default">
                <div class="panel-heading"><i class="glyphicon glyphicon-th-list"></i> 权限菜单列表
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i
                            class="glyphicon glyphicon-question-sign"></i></div>
                </div>
                <div class="panel-body">
                    <ul id="authTreeDemo" class="ztree"></ul>
                </div>
                <tfoot>
                <button id="addRootAuthBtn" type="button" class="btn btn-primary"><i
                        class="glyphicon glyphicon-plus"></i> 添加
                </button>
                </tfoot>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-auth-add.jsp" %>
<%@include file="/WEB-INF/modal-auth-confirm.jsp" %>
<%@include file="/WEB-INF/modal-auth-edit.jsp" %>
<%@include file="/WEB-INF/modal-auth-root-add.jsp" %>
</body>
</html>

